/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

import * as fse from 'fs-extra';
import path from 'path';

import { CommonConst } from '../../../const/common-const.js';
import { TaskNames } from '../../common/task-names.js';
import { processResourceConfig } from '../../har/process-oh-package-json.js';
import { TargetTaskService } from '../../service/target-task-service.js';
import { OhosHarTask } from '../../task/ohos-har-task.js';
import LegacyFATask = TaskNames.LegacyFATask;
import { LegacyPreBuild } from '../legacy-pre-build.js';

/**
 * har模块需要处理指定resource目录
 * 当前该任务执行处理很快,先不需要做增量
 */
export class LegacyProcessOHPackageJson extends OhosHarTask {
  private readonly generateOhPackageJsonFilePath: string;

  constructor(taskService: TargetTaskService) {
    super(taskService, LegacyFATask.PROCESS_OH_PACKAGE_JSON);
    this.generateOhPackageJsonFilePath = path.resolve(this.pathInfo.getGeneratePmDir(), CommonConst.OH_PACKAGE_JSON5);
  }

  protected doTaskAction(): void {
    processResourceConfig(this.targetService, this.packageJsonObj);
    fse.outputJSONSync(this.generateOhPackageJsonFilePath, this.packageJsonObj);
  }

  initTaskDepends(): void {
    this.declareDepends(LegacyPreBuild.name);
  }
}
